package com.letoken.platform.repo.ecommerce.dao;

import com.letoken.platform.repo.ecommerce.po.EcommerceUserMasterOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.letoken.platform.repo.ecommerce.po.EcommerceUserOrder;
import com.letoken.platform.repo.ecommerce.vo.UserAfterOrderCenterVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 主订单 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-11-23
 */
public interface EcommerceUserMasterOrderMapper extends    BaseMapper<EcommerceUserMasterOrder> {


    @Select("select * from  ecommerce_user_order where father_no = #{fatherNo} ")
    List<EcommerceUserOrder> subOrders(@Param("fatherNo") String fatherNo);


    @Select("select * from ecommerce_user_master_order where father_no = #{fatherNo} ")
    EcommerceUserMasterOrder selectByFatherNo(@Param("fatherNo") String fatherNo);


    @Select("select m.shipping from ecommerce_user_master_order m inner join ecommerce_user_order o on m.father_no = o.father_no  where o.id = #{orderId} ")
    BigDecimal selectShipping(@Param("orderId") Integer orderId);

    @Select("select m.shipping_cap from ecommerce_user_master_order m inner join ecommerce_user_order o on m.father_no = o.father_no  where o.id = #{orderId} ")
    BigDecimal selectShippingCAP(@Param("orderId") Integer orderId);


    @Select("SELECT o.id ,a.id as afterId ,a.before_status ,a.refund_status,a.status  from  ecommerce_user_order o  left join ecommerce_after_order a on o.id  = a.order_id  where o.father_no = #{fatherNo}   and o.id != #{orderId}  ")
    List<UserAfterOrderCenterVo> needRefundShipping(@Param("fatherNo") String fatherNo,@Param("orderId") Integer orderId);


    // 返回1表示不是最后一笔未完成的子订单
    @Select("SELECT 1  from  ecommerce_user_order o    where o.father_no = #{fatherNo}   and o.id != #{orderId}   and o.status !=3 and o.status !=4 limit 1 ")
    Integer isLast(@Param("fatherNo") String fatherNo,@Param("orderId") Integer orderId);

    @Select("select * from ecommerce_user_master_order where id = #{id} for update")
    EcommerceUserMasterOrder selectForUpdate(@Param("id") Integer id);


    @Update("update ecommerce_user_master_order set reviewed = #{reviewed} where father_no = #{fatherNo}")
    int updateMasterReviewed(@Param("fatherNo")String fatherNo,@Param("reviewed") Integer reviewed);



    @Select("select o.id,  IF((select count(1)  - sum(a.reviewed)  from ecommerce_user_order a where a.father_no = o.father_no ) > 0, IF( (select  sum(a.reviewed)    from ecommerce_user_order a where a.father_no = o.father_no ) >0 ,2,0 ), 1) as reviewed   from ecommerce_user_master_order o where o.father_no  in (${nos})   ")
    List<EcommerceUserMasterOrder> fathers(@Param("nos") String nos);


    @Select("select o.id,  IF((select count(1)  - sum(a.reviewed)  from ecommerce_user_order a where a.father_no = o.father_no ) > 0, IF( (select  sum(a.reviewed)    from ecommerce_user_order a where a.father_no = o.father_no ) >0 ,2,0 ), 1) as reviewed   from ecommerce_user_master_order o where o.father_no  = #{nos}   ")
    EcommerceUserMasterOrder fatherOne(@Param("nos") String nos);



    @Select("select o.id,  IF((select count(1)  - sum(a.reviewed)  from ecommerce_user_order a where a.father_no = o.father_no ) > 0, IF( (select  sum(a.reviewed)    from ecommerce_user_order a where a.father_no = o.father_no ) >0 ,2,0 ), 1) as reviewed   from ecommerce_user_master_order o   ")
    List<EcommerceUserMasterOrder> allFathers();


    void updateBatchReviewedById(@Param("list") List<EcommerceUserMasterOrder> list);


    @Select("select * from ecommerce_user_master_order where status != 3  and status != 4 ")
    List<EcommerceUserMasterOrder> masterOrders();
}
